package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //写入基础信息到setmeal表中
        this.save(setmealDto);
        //获取到当前套餐id
        Long id = setmealDto.getId();
        //将菜品放入到集合中
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();

        for (SetmealDish dish : dishes) {
            //将菜品id与套餐id对应
            dish.setSetmealId(id);
        }
        //批量存储
        setmealDishService.saveBatch(dishes);

    }

    @Override
    public void removeByIdWithDish(Long[] ids) {
        //看看套餐表是不是 起售状态 不是起售状态才能删除0为停售1为起售
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //对应的id and id in (?,?,?)
        wrapper.in(Setmeal::getId,ids);
        //where status = 1
        wrapper.eq(Setmeal::getStatus,1);
        //获取起售套餐的个数
        int count = this.count(wrapper);
        //如果套餐为起售状态 则不能删除
        if (count > 0)
            throw new CustomException("有套餐为起售状态不能删除");
        //然后根据套餐的id删除关联菜品表中的关联数据
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        //where setmeal_id = ?(套餐的关联id)
        wrapper1.eq(SetmealDish::getSetmealId,ids);
        //删除套餐关联菜品信息
        setmealDishService.remove(wrapper1);
        //无起售状态删除套餐信息
        this.removeByIds(Arrays.asList(ids));

    }

    @Override
    public SetmealDto showById(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        //根据id先查出套餐基本信息
        Setmeal setmeal = this.getById(id);
        //将setmale写入到Dto中
        BeanUtils.copyProperties(setmeal,setmealDto);
        //根据id查出菜品关联信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //重新写入套餐基本数据
        this.updateById(setmealDto);
        //清除套餐关联菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        //获取对应的关联表数据
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        //进行删除
        setmealDishService.remove(wrapper);
        //将获取到的关联菜品信息 存入集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            //存入相关 套餐与菜品对应id
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //批量存储
        setmealDishService.saveBatch(setmealDishes);
    }
}
